package com.mc.moring.widge;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.mc.moring.BafBase.NArrayAdapter;
import com.mc.moring.R;
import java.util.ArrayList;
import java.util.List;


/**
 * 金额
 */
public class JPriceDialog extends Dialog {

    private TextView btnConfirm;
    private ImageView btnCancel;
    private ListView listView;

    public interface DialogCallback {

        /**
         * 列表选定回调
         *
         * @param selectedData 选定的列表项数据
         */
        void confirm(String selectedData);
    }

    private static Context mContext;
    private static DialogCallback callback;
    private static List<String> mListData = new ArrayList<>();
    private static String selectedData;
    private MyAdapter adapter;

    public JPriceDialog(Context context, List<String> listData, DialogCallback onClickListener) {
        super(context, R.style.JDialog_Them);
        mContext = context;
        mListData = listData;
        callback = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_jh_price);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    public void initView() {
        listView = findViewById(R.id.listView);
        btnCancel = findViewById(R.id.btn_close);
        btnConfirm = findViewById(R.id.btn_confirm);
    }

    public void initData() {
        adapter = new MyAdapter(mContext, (ArrayList<String>) mListData);
        listView.setAdapter(adapter);
    }

    public void initEvent() {
        listView.setOnItemClickListener((arg0, view, position, arg3) -> {
            adapter.setSelectPosition(position);
            selectedData = mListData.get(position);
        });
        btnConfirm.setOnClickListener(v -> {
            callback.confirm(selectedData);
            dismiss();
        });
        btnCancel.setOnClickListener(v -> dismiss());
    }

    public class MyAdapter extends NArrayAdapter {

        private int defaultSelect = 0;

        /**
         * 构造函数
         *
         * @param context 上下文
         * @param data    数据
         */
        MyAdapter(Context context, ArrayList<String> data) {
            super(context, data);
        }

        @Override
        public View getListItemView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_jh_price, null, false);
                viewHolder = new ViewHolder();

                // 初始化空间
                viewHolder.itemDataTv = convertView.findViewById(R.id.tv_name);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            // 设置数据
            if (mData != null && !mData.isEmpty()) {
                viewHolder.itemDataTv.setText(mData.get(position).toString());
            }

            if (position == defaultSelect) {
                viewHolder.itemDataTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            } else {
                viewHolder.itemDataTv.setTextColor(ContextCompat.getColor(mContext, R.color.grey71));
            }
            return convertView;
        }

        class ViewHolder {
            TextView itemDataTv;
        }

        public void setSelectPosition(int position) {
            if (!(position < 0 || position > mData.size())) {
                defaultSelect = position;
                notifyDataSetChanged();
            }
        }

        public int getSelectPosition() {
            return defaultSelect;
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}

